﻿using B.S.BasicData.Api.Read.Applications.Commands.Accounts;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastcuture.Commons;
using B.S.BasicData.Infrastcuture.Interfaces;
using MediatR;

namespace B.S.BasicData.Api.Read.Applications.CommandHandlers.Accounts
{
    public class NlogQueryCommandHandler : IRequestHandler<NlogQueryCommand, ApiResult<ApiPaging<NlogInfo>>>
    {
        private readonly IBaseRepository<NlogInfo> baseRepository;

        public NlogQueryCommandHandler(IBaseRepository<NlogInfo> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<ApiResult<ApiPaging<NlogInfo>>> Handle(NlogQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<NlogInfo>> result = new ApiResult<ApiPaging<NlogInfo>>();
            result.Code = ResultCode.SearchSuccess;
            result.Message = ResultCode.SearchSuccess.Description();
            var list=baseRepository.GetAll();
            int tocom = list.Count();
            int topae=(int)Math.Ceiling(1.0*tocom/request.PageSize);
            list = list.OrderBy(x => x.NlogId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            ApiPaging<NlogInfo> page=new ApiPaging<NlogInfo>(); 
            page.TotoCom = tocom;
            page.TotoPage= topae;
            page.Data = list.ToList();
            result.Data = page;
            return Task.FromResult(result);
        }
    }
}
